package web.admin.shop;


import domain.shop.DeliveryMethod;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.ShopAdminService;

import java.util.List;
import java.util.MissingResourceException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class provides administration of delivery methods
 */
public final class DeliveryMethodAdminPanel extends Panel {

    @SpringBean
    private ShopAdminService shopAdminService;
    private Panel empty = new EmptyPanel("current");
    private LoadableDetachableModel<List<DeliveryMethod>> deliveryMethodsModel;
    private Panel current = empty;
    private static Logger logger = Logger.getLogger(DeliveryMethodAdminPanel.class.getName());

    public DeliveryMethodAdminPanel(String id) {
        super(id);
        add(current);
        deliveryMethodsModel =
                new LoadableDetachableModel<List<DeliveryMethod>>() {

                    @Override
                    protected List<DeliveryMethod> load() {
                        return shopAdminService.findAllDeliveryMethods();
                    }
                };
        add(new ListView<DeliveryMethod>("deliveryMethodsList", deliveryMethodsModel) {

            @Override
            protected void populateItem(final ListItem<DeliveryMethod> item) {
                item.add(new Form("form") {

                    {
                        add(new Label("objectId", new PropertyModel(item.getModel(), "id")));
                        add(new Label("name", new PropertyModel(item.getModel(), "name")));
                        add(new Link("edit") {

                            @Override
                            public void onClick() {
                                replacePanel(item.getModelObject().getId());
                            }
                        });
                    }

                    @Override
                    protected void onSubmit() {
                        try {
                            shopAdminService.deleteDeliveryMethod((DeliveryMethod) item.getModelObject());
                            deliveryMethodsModel.detach();
                            replacePanelToEmpty();
                            info(getLocalizer().getString("shop.success.deliveryMethod.delete", DeliveryMethodAdminPanel.this));
                        } catch (MissingResourceException e) {
                            logger.log(Level.SEVERE, null, e);
                            error(getLocalizer().getString("system.error", this));
                        }

                    }
                });
            }
        });

        Form form = new Form("form");
        Link addDeliveryMethod = new Link("addDeliveryMethod") {

            @Override
            public void onClick() {
                DeliveryMethod deliveryMethod = null;
                try {
                    deliveryMethod = shopAdminService.createDeliveryMethod();
                    replacePanel(deliveryMethod.getId());
                    info(getLocalizer().getString("shop.success.deliveryMethod.create", DeliveryMethodAdminPanel.this));
                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                    error(getLocalizer().getString("system.error", this));
                }
            }
        };
        form.add(addDeliveryMethod);
        add(form);
    }


    public void replacePanelToEmpty() {
        current.replaceWith(empty);
        current = empty;
    }

    public void replacePanel(Long id) {
        DeliveryMethodPanel deliveryMethodPanel = new DeliveryMethodPanel("current", id, this);
        current.replaceWith(deliveryMethodPanel);
        current = deliveryMethodPanel;
    }
}
